namespace eval autoaway {
    variable options
    variable savestatus ""
    variable savetext   ""
    variable savepriority 0
    variable pressed 0
}


proc autoaway::load {} {
    global tcl_platform
    global idle_command

    if {[catch {tk inactive}] || ([tk inactive] < 0)} {
	switch -- $tcl_platform(platform) {
	    macintosh {
		set idle_command [namespace current]::AquaIdleTime
	    }

	    unix {
		if {$::aquaP} {
		    set idle_command [namespace current]::AquaIdleTime
		} elseif {[catch { package require tkXwin }]} {
		    return
		} else {
		    set idle_command tkXwin::idletime
		}
	    }
	    windows {
		if {![catch { package require tkinactive }]} {
		    set idle_command tkinactive
		} elseif {![catch { package require tclWinidle }]} {
		    set idle_command tclWinidle::idletime
		} else {
		    return
		}
	    }
	    default {
		return
	    }
	}
    } else {
	set idle_command {tk inactive}
    }

    custom::defgroup AutoAway \
	[::msgcat::mc "Options for module that automatically marks\
		       you as away after idle threshold."] \
	-group Tkabbur

    custom::defvar options(awaytime) 0 \
	[::msgcat::mc "Idle threshold in minutes after that\
		       Tkabbur marks you as away."] \
	-group AutoAway -type integer

    custom::defvar options(status) \
	[::msgcat::mc "Automatically away due to idle"] \
	[::msgcat::mc "Text status, which is set when\
		       Tkabbur is moving to away state."] \
	-group AutoAway -type string
    custom::defvar options(status_seen) \
	[::msgcat::mc "Last seen: "] \
	[::msgcat::mc "Optional text, which indicates when Tkabbur moved to away state."] \
	-group AutoAway -type string
    custom::defvar options(status_status) \
	[::msgcat::mc "Last status: "] \
	[::msgcat::mc "Optional text, which indicates your text status when Tkabbur moved to away state."] \
	-group AutoAway -type string


    hook::add connected_hook    [namespace current]::after_idle
    hook::add disconnected_hook [namespace current]::after_idle_cancel
}


proc autoaway::after_idle {args} {
    after cancel [namespace current]::after_idle
    after_idle_aux
    if {$::aquaP} {
        set msec 1000
    } else {
        set msec 250
    }
    after $msec [namespace current]::after_idle
}

proc autoaway::after_idle_cancel {args} {
    if {[jlib::connections] == {}} {
	after cancel [namespace current]::after_idle
    }
}

proc autoaway::end_autoaway {w} {
	variable pressed
	variable savestatus
	variable savetext
	variable savepriority
	variable options

	global userstatus textstatus
	global userpriority
 
 	if {!$pressed} {
 		return
 	}
 	set pressed 0
 	destroy $w
 	set userpriority $savepriority
 	set textstatus $savetext
 	set savetext ""
         set userstatus $savestatus
         set savestatus ""
         set_status [::msgcat::mc "Returning from auto-away"]
 }
 proc autoaway::show_dialog {} {
 	variable pressed
 	variable w

 	if {$pressed} {
 		return
 	}
 	set pressed 1
 	set w .autoawaydialog
 	if {[winfo exists $w]} {
 		focus -force $w
 		return
 	}
 	Dialog $w -title [::msgcat::mc "You are automatically away"] \
 		-modal local -separator 1 -anchor e \
 		-default 0 -cancel 0
 	set gf [$w getframe]
     	label $gf.txt -text [::msgcat::mc "You were idle for too long time, so Tkabbur detected you to be automatically away.\nYour interaction with tkabber is disabled until you press Done and get back to internet life."]
 	grid $gf.txt -row 0 -column 0 -sticky e
 
 	$w add -text [::msgcat::mc "Done"] \
 		-command [list [namespace current]::end_autoaway $w]
 	$w draw
 
 }

proc autoaway::after_idle_aux {} {
    variable options
    variable savestatus
    variable savetext
    variable savepriority
    variable pressed
    global idle_command
    global userstatus textstatus userpriority

    if {$options(awaytime) <= 0} {
        return
    }

    set idletime [eval $idle_command]

    if {$idletime < [expr {$options(awaytime)*60*1000}]} {
    	return
    }

    switch -- $userstatus {
        available -
        chat {
            set savestatus $userstatus
	    set savetext $textstatus
	    set savepriority $userpriority
	    set savetime [clock format [clock seconds]]
	    set texta "$options(status)\n$options(status_seen)$savetime"
	    if {$textstatus!=""} {
	    	set etxta "$texta\n$options(status_status)$textstatus"
	    }
	    set textstatus $texta
	    set userpriority 0
            set userstatus away
            set_status [::msgcat::mc "Starting auto-away"]
	    show_dialog
            return
        }

        default {
        }
    }

    if {![cequal $savestatus ""]} {
        set_status [format [::msgcat::mc "Idle for %s"] [format_time [expr {$idletime/1000}]]]
    }
}

proc autoaway::AquaIdleTime {} {
    catch { set line [read [set fd [open {|ioreg -x -c IOHIDSystem}]]] }
    close $fd

    expr {"0x[lindex [regexp -inline {"HIDIdleTime" = (?:0x|<)([[:xdigit:]]+)} \
                 $line] 1]"/1000000}
}

autoaway::load

# vim:ts=8:sw=4:sts=4:noet
